<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

		
function authcode ($string, $operation, $key = 'i7GcN7Q4bb8b8fr0cfy2zer7SfFe3djdJ4F4T5q9688fJbn1r9Vdu886W8Pbv7ff') {
	$key_length = strlen($key);
	$string = $operation == 'DECODE' ? base64_decode($string) : substr(md5($string.$key), 0, 8).$string;
	
	$string_length = strlen($string);
	$rndkey = $box = array();
	$result = '';

	for($i = 0; $i <= 255; $i++) {
		$rndkey[$i] = ord($key[$i % $key_length]);
		$box[$i] = $i;
	}

	for($j = $i = 0; $i < 256; $i++) {
		$j = ($j + $box[$i] + $rndkey[$i]) % 256;
		$tmp = $box[$i];
		$box[$i] = $box[$j];
		$box[$j] = $tmp;
	}
	for($a = $j = $i = 0; $i < $string_length; $i++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box[$a]) % 256;
		$tmp = $box[$a];
		$box[$a] = $box[$j];
		$box[$j] = $tmp;
		$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
	}
	if($operation == 'DECODE') {
		if(substr($result, 0, 8) == substr(md5(substr($result, 8).$key), 0, 8)) {
			return substr($result, 8);
		} else {
			return '';
		}
	} else {
		return str_replace('=', '', base64_encode($result));
	}
}

function random($length, $numeric = 0) {
	mt_srand((double)microtime() * 1000000);
	if($numeric) {
		$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
	} else {
		$hash = '';
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++) {
			$hash .= $chars[mt_rand(0, $max)];
		}
	}
	return $hash;
}




function array_to_json($array,$t=1)
{
	if((is_array($array) and count($array)>0)==false)Return "{}";
	$ts=str_repeat("\t",$t);
	$next_t=$t+1;
	$json="{";
	foreach($array as $key=>$val)
	{
		if(is_array($val)==false)
		{
			
			$val=js_string($val);
			$json.="\n{$ts}'{$key}':{$val},";
		}
		else
		{
			$_json = array_to_json($val,$next_t);
			$json .= "\n{$ts}'{$key}':\n{$ts}{$_json},";
		}
	}
	$json=rtrim($json,',');
	$ts=str_repeat("\t",$t-1);
	$json.="\n$ts}";
	Return $json;
}

function debug($mixed,$halt=true)
{
	static $num=1;
	if (function_exists("debug_backtrace"))
	{
		$debug=debug_backtrace();
		echo "<div style='background:#FF6666;color:#fff;margin-top:5px;padding:5px'>".$num++.".debug position: {$debug[0]['file']}({$debug[0]['line']})</div>";
	}
	echo "<div style='border:1px solid #ff6666;background:#fff;padding:10px'>";
	if (is_array($mixed) or is_object($mixed))
	{
		echo str_replace(array("&lt;?php","?&gt;"),'',highlight_string("<?php\r\n".var_export($mixed,true).";\r\n?>",true));
	}
	else
	{
		var_dump($mixed);
	}
	echo "</div>";
	$halt && exit;
}

if (function_exists('iconv')==false)
{
	
	function iconv($in_charset,$out_charset,$str)
	{
		if(strtoupper($in_charset)!=strtoupper($out_charset))
		{
			static $CharEncoding;
			if($CharEncoding===null)
			{
				if(false!==strpos($out_charset,'/'.'/')) $out_charset = str_replace(array('/'.'/IGNORE','/'.'/TRANSLIT'),'',$out_charset);
				require_once(INCLUDE_PATH.'encoding/chinese.class.php');
				$CharEncoding=new Chinese($in_charset,$out_charset);
			}
			Return $CharEncoding->Convert($str);
		}
		return $str;	
	}
}

function is_image($filename,$allow_types=array('gif'=>1,'jpg'=>1,'png'=>1,'bmp'=>1)) {
	if(!is_file($filename)) {
		return false;
	}
	
	$imagetypes = array('1'=>'gif','2'=>'jpg','3'=>'png','4'=>'swf','5'=>'psd','6'=>'bmp','7'=>'tiff','8'=>'tiff','9'=>'jpc','10'=>'jp2','11'=>'jpx','12'=>'jb2','13'=>'swc','14'=>'iff','15'=>'wbmp','16'=>'xbm',);
	if(!$allow_types) {
		$allow_types = array('gif'=>1,'jpg'=>1,'png'=>1,'bmp'=>1);
	}
	$typeid = 0;
	$imagetype = '';
	if(function_exists('exif_imagetype')) {
		$typeid = exif_imagetype($filename);
	} elseif (function_exists('getimagesize')) {
		$_tmps = getimagesize($filename);
		$typeid = (int) $_tmps[2];
	} else {
		if(($fh = @fopen($filename, "rb"))) {
			$strInfo = unpack("C2chars", fread($fh,2));
			fclose($fh);
			$fileTypes = array(7790=>'exe',7784=>'midi',8297=>'rar',255216=>'jpg',7173=>'gif',6677=>'bmp',13780=>'png',);
			$imagetype = $fileTypes[intval($strInfo['chars1'] . $strInfo['chars2'])];
		}
	}
	$file_ext = strtolower(trim(substr(strrchr($filename, '.'), 1)));
	if($typeid > 0) {
		$imagetype = $imagetypes[$typeid];		
	} else {
		if(!$imagetype) {
			$imagetype = $file_ext;
		}
	}
	
	if($allow_types && $file_ext && $imagetype && isset($allow_types[$file_ext]) && isset($allow_types[$imagetype])) {
		return true;
	}
	
	return false;
}
function gallery_url($gallery_path,$size = 'original')
{
	$path = 'uploads/' . $gallery_path;
	if('small' == $size)
	{
		$suffix = '.small.jpg';
	}
	elseif('medium' == $size)
	{
		$suffix = '.medium.jpg';
	}
	elseif('large' == $size)
	{
		$suffix = '.large.jpg';
	}
	else
	{
		$suffix = '';
	}
	return site_url('uploads/'. $gallery_path. $suffix);
}
function imageurl($imgsrc, $type='')
{
	$img_urls = explode('|', $imgsrc);
	if(count($img_urls) > 1)
	{
		$file = $img_urls[0];
		$pre = '';
		if ($type == 's')
		{
			$pre = 's-';
		}
		if ($type == 'm')
		{
			$pre = 'm-';
		}
		return IMAGE_URL_PATH.'product/'.$pre.$file;
	}
	else 
	{
		return '';
	}
	
}
function resize_image($file_path,$thumb_file_path,$width,$height,$cut=false)
{	
	return makethumb($file_path,$thumb_file_path,$width,$height);
}
function makethumb($srcfile,$dstfile,$thumbwidth,$thumbheight,$maxthumbwidth=0,$maxthumbheight=0) {
		if (!is_file($srcfile)) {
			return '';
		}
	
		$tow = $thumbwidth;
		$toh = $thumbheight;
		/*if($tow < 30) {
			$tow = 30;
		}
		if($toh < 30) {
			$toh = 30;
		}*/
	
		$make_max = 0;
		$maxtow = $maxthumbwidth;
		$maxtoh = $maxthumbheight;
		if($maxtow >= 300 && $maxtoh >= 300) {
			$make_max = 1;
		}
		
			$im = '';
		if($data = getimagesize($srcfile)) {
			if($data[2] == 1) {
				$make_max = 0;			
				if(function_exists("imagecreatefromgif")) {
					$im = imagecreatefromgif($srcfile);
				}
			} elseif($data[2] == 2) {
				if(function_exists("imagecreatefromjpeg")) {
					$im = imagecreatefromjpeg($srcfile);
				}
			} elseif($data[2] == 3) {
				if(function_exists("imagecreatefrompng")) {
					$im = imagecreatefrompng($srcfile);
				}
			}
		}
		if(!$im) return '';
		
		$srcw = imagesx($im);
		$srch = imagesy($im);
		
		if($srcw < $tow) {
			$tow = $srcw;
		}
		if($srch < $toh) {
			$toh = $srch;
		}
		
		$towh = $tow/$toh;
		$srcwh = $srcw/$srch;
		if($towh <= $srcwh){
			$ftow = $tow;
			$ftoh = $ftow*($srch/$srcw);
			
			$fmaxtow = $maxtow;
			$fmaxtoh = $fmaxtow*($srch/$srcw);
		} else {
			$ftoh = $toh;
			$ftow = $ftoh*($srcw/$srch);
			
			$fmaxtoh = $maxtoh;
			$fmaxtow = $fmaxtoh*($srcw/$srch);
		}
		if($srcw <= $maxtow && $srch <= $maxtoh) {
			$make_max = 0;	
		}
		if(1) {
//		if($srcw >= $tow || $srch >= $toh) {
			if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh)) {
				imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
							if($make_max && @$maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh)) {
					imagecopyresampled($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
				}
			} elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh)) {
				imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
							if($make_max && @$maxni = imagecreate($fmaxtow, $fmaxtoh)) {
					imagecopyresized($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
				}
			} else {
				return '';
			}
			if(function_exists('imagejpeg')) {
				imagejpeg($ni, $dstfile);
							if($make_max) {
					imagejpeg($maxni, $srcfile);
				}
			} elseif(function_exists('imagepng')) {
				imagepng($ni, $dstfile);
							if($make_max) {
					imagepng($maxni, $srcfile);
				}
			}
			imagedestroy($ni);
			if($make_max) {
				imagedestroy($maxni);
			}
		}
		imagedestroy($im);
	
		if(!is_file($dstfile)) {
			return '';
		} else {
			return $dstfile;
		}
}
